package com.example.myapplication.adapter.recyclerViewAdapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.example.myapplication.activity.BaseOnclickListener;
import com.example.myapplication.activity.LoadingActivity;
import com.example.myapplication.R;
import com.example.myapplication.activity.SingerDetailActivity;
import com.example.myapplication.adapter.viewPagerAdapter.RecommendSongAdapter;
import com.example.myapplication.entity.Artist;
import com.example.myapplication.old.entity.Game;

import java.util.List;

public class RecommendAdapter extends RecyclerView.Adapter<RecommendAdapter.RecommendHolder> {
    private Context context;
    private List<Game> list ;
    private RecyclerView recycler_hor;
    private ViewPager view_pager_song;
    private RecyclerHorAdapter horAdapter;
    private RecyclerView.LayoutManager manager_hor;
    private FragmentManager fragmentManager;
    private List<Artist> artistList;
    private int count = 1;
    private String[]reasonList = {"猜你喜欢","编辑推荐","大家都在听","熟悉的味道 熟悉的音乐","无处不在的音乐节奏"};
    private String[]reason = {"热门歌手","这些优质歌手，太可了","宝藏歌手集合","热门推荐","歌手推荐"};
    private static int limit=0;
    public RecommendAdapter(Context context, FragmentManager fragmentManager) {
        this.context = context;
        this.fragmentManager = fragmentManager;
    }

    @NonNull
    @Override
    public RecommendHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recommend,parent,false);
        
        return new RecommendHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecommendHolder holder, int position) {
        holder.tv_desc.setText(reason[(int) (Math.random()*5)]);
        holder.reason.setText(reasonList[(int) (Math.random()*5)]);
        holder.view_pager_song.setId(1+position);

    }

    @Override
    public int getItemCount() {
        return count>4? 4 :count;
    }

    class RecommendHolder extends RecyclerView.ViewHolder{
        TextView tv_desc;
        RecyclerView recycler_hor;
        ViewPager view_pager_song;
        TextView refresh;
        TextView tips;
        TextView reason;
        public RecommendHolder(@NonNull View itemView) {
            super(itemView);
            tv_desc = itemView.findViewById(R.id.tv_desc);
            recycler_hor = itemView.findViewById(R.id.recycler_hor);
            view_pager_song = itemView.findViewById(R.id.view_pager_song);
            reason  = itemView.findViewById(R.id.reason);
            artistList = LoadingActivity.artistList;
            manager_hor = new LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false);
            horAdapter = new RecyclerHorAdapter(context);
            recycler_hor.setLayoutManager(manager_hor);
            recycler_hor.setAdapter(horAdapter);
            horAdapter.setData(artistList);
            RecommendSongAdapter recommendSongAdapter  = new RecommendSongAdapter(fragmentManager);
            view_pager_song.setOffscreenPageLimit(3);
            view_pager_song.setAdapter(recommendSongAdapter);
            refresh = itemView.findViewById(R.id.refresh);
            tips = itemView.findViewById(R.id.tips);
            refresh.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(count>=4){
                        Toast toast = Toast.makeText(context, "没有更多了", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER, 0, 0);
                        toast.show();
                    }
                    count++;
                    tips.setVisibility(View.GONE);
                    refresh.setVisibility(View.GONE);

                }
            });

            horAdapter.setBaseOnclickListener(new BaseOnclickListener() {
                @Override
                public void onclick(int position) {
                    Intent intent = new Intent(context, SingerDetailActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("singer",  artistList.get(position));
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                }
            });

        }
    }

}
